extends Node
class_name UIAnimator

func animate_panel_appear(panel: Control, duration: float = 0.3):
	panel.modulate.a = 0.0
	panel.scale = Vector2(0.8, 0.8)
	panel.visible = true
	
	var tween = panel.get_tree().create_tween()
	tween.set_parallel(true)
	tween.tween_property(panel, "modulate:a", 1.0, duration)
	tween.tween_property(panel, "scale", Vector2.ONE, duration).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_BACK)

func animate_panel_disappear(panel: Control, duration: float = 0.3):
	var tween = panel.get_tree().create_tween()
	tween.set_parallel(true)
	tween.tween_property(panel, "modulate:a", 0.0, duration)
	tween.tween_property(panel, "scale", Vector2(0.8, 0.8), duration).set_ease(Tween.EASE_IN)
	tween.chain().tween_callback(panel.hide)

func animate_button_hover(button: Button):
	var tween = button.get_tree().create_tween()
	tween.tween_property(button, "scale", Vector2(1.1, 1.1), 0.1).set_ease(Tween.EASE_OUT)

func animate_button_unhover(button: Button):
	var tween = button.get_tree().create_tween()
	tween.tween_property(button, "scale", Vector2.ONE, 0.1).set_ease(Tween.EASE_OUT)

func animate_button_press(button: Button):
	var tween = button.get_tree().create_tween()
	tween.tween_property(button, "scale", Vector2(0.95, 0.95), 0.05)
	tween.tween_property(button, "scale", Vector2.ONE, 0.05)

func animate_gold_change(label: Label, old_value: int, new_value: int, duration: float = 0.5):
	var tween = label.get_tree().create_tween()
	
	# Flash effect
	tween.tween_property(label, "modulate", Color(1.2, 1.2, 0.5), 0.1)
	tween.tween_property(label, "modulate", Color.WHITE, 0.2)
	
	# Count up/down animation
	var counter = { "value": old_value }
	tween.parallel().tween_property(counter, "value", new_value, duration)
	tween.set_process_mode(Tween.TWEEN_PROCESS_IDLE)
	tween.set_loops(0)
	
	# Update label during animation
	var update_func = func():
		label.text = "Gold: " + str(int(counter.value))
	
	tween.tween_callback(update_func).set_delay(0.01)

func animate_round_start(round_label: Label):
	round_label.scale = Vector2(2, 2)
	round_label.modulate.a = 1.0
	
	var tween = round_label.get_tree().create_tween()
	tween.set_parallel(true)
	tween.tween_property(round_label, "scale", Vector2.ONE, 0.5).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_ELASTIC)
	tween.chain().tween_property(round_label, "modulate:a", 1.0, 1.0)

func animate_phase_change(phase_label: Label, new_text: String):
	var tween = phase_label.get_tree().create_tween()
	
	# Fade out
	tween.tween_property(phase_label, "modulate:a", 0.0, 0.2)
	
	# Change text
	tween.tween_callback(func(): phase_label.text = new_text)
	
	# Fade in with pulse
	tween.tween_property(phase_label, "modulate:a", 1.0, 0.2)
	tween.tween_property(phase_label, "scale", Vector2(1.2, 1.2), 0.1)
	tween.tween_property(phase_label, "scale", Vector2.ONE, 0.1)

func animate_synergy_activation(synergy_panel: Control):
	var original_pos = synergy_panel.position
	
	var tween = synergy_panel.get_tree().create_tween()
	tween.tween_property(synergy_panel, "position:x", original_pos.x - 10, 0.1)
	tween.tween_property(synergy_panel, "position:x", original_pos.x, 0.3).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_ELASTIC)
	
	# Glow effect
	tween.parallel().tween_property(synergy_panel, "modulate", Color(1.2, 1.2, 1.0), 0.2)
	tween.tween_property(synergy_panel, "modulate", Color.WHITE, 0.2)

func animate_shop_refresh(shop_container: Control):
	var tween = shop_container.get_tree().create_tween()
	
	# Spin animation
	tween.tween_property(shop_container, "rotation", deg_to_rad(360), 0.3)
	tween.tween_callback(func(): shop_container.rotation = 0)
	
	# Scale bounce
	tween.parallel().tween_property(shop_container, "scale", Vector2(0.8, 0.8), 0.15)
	tween.tween_property(shop_container, "scale", Vector2.ONE, 0.15).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_BACK)

func animate_unit_purchase(button: Button):
	var tween = button.get_tree().create_tween()
	
	# Shrink and fade
	tween.set_parallel(true)
	tween.tween_property(button, "scale", Vector2.ZERO, 0.3).set_ease(Tween.EASE_IN).set_trans(Tween.TRANS_BACK)
	tween.tween_property(button, "modulate:a", 0.0, 0.3)

func animate_damage_shake(node: Node3D, intensity: float = 0.1, duration: float = 0.2):
	var original_pos = node.position
	var shake_count = 5
	var tween = node.get_tree().create_tween()
	
	for i in range(shake_count):
		var offset = Vector3(
			randf_range(-intensity, intensity),
			0,
			randf_range(-intensity, intensity)
		)
		tween.tween_property(node, "position", original_pos + offset, duration / shake_count)
	
	tween.tween_property(node, "position", original_pos, duration / shake_count)

func animate_victory_banner(message: String = "VICTORY!"):
	var banner = Label.new()
	banner.text = message
	banner.add_theme_font_size_override("font_size", 72)
	banner.add_theme_color_override("font_color", Color(1.0, 0.8, 0.0))
	banner.add_theme_color_override("font_shadow_color", Color(0.2, 0.1, 0.0))
	banner.add_theme_constant_override("shadow_offset_x", 4)
	banner.add_theme_constant_override("shadow_offset_y", 4)
	
	var canvas = CanvasLayer.new()
	canvas.add_child(banner)
	
	banner.set_anchors_and_offsets_preset(Control.PRESET_CENTER)
	banner.position.y -= 100
	
	get_tree().root.add_child(canvas)
	
	# Animate
	banner.scale = Vector2.ZERO
	banner.modulate.a = 1.0
	
	var tween = banner.get_tree().create_tween()
	tween.tween_property(banner, "scale", Vector2(1.5, 1.5), 0.5).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_ELASTIC)
	tween.tween_property(banner, "scale", Vector2.ONE, 0.2)
	tween.tween_interval(1.0)
	tween.tween_property(banner, "modulate:a", 0.0, 0.5)
	tween.tween_callback(canvas.queue_free)